package com.bodacn.keypoint.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bodacn.keypoint.R;
import com.bodacn.keypoint.bean.TaiorCheckBean;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by KangJunjie on 2020/6/18.
 */
public class TaiorCheckAdapter extends BaseAdapter {
    private Context context;
    private List<TaiorCheckBean> dataBeans;
    private OrderDetialOn orderDetialOn;

    public TaiorCheckAdapter(Context context, List<TaiorCheckBean> beanList, OrderDetialOn orderDetialOn) {
        this.context = context;
        this.dataBeans = beanList;
        this.orderDetialOn = orderDetialOn;
    }

    @Override
    public int getCount() {
        return dataBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return dataBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_taior_gr, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (dataBeans.size() > 0) {
            viewHolder.tvPlaceNo.setText(dataBeans.get(position).getPlaceNo());
            if (!TextUtils.isEmpty(dataBeans.get(position).getOrderNo())) {
                viewHolder.tvOrderNo.setText(dataBeans.get(position).getOrderNo());
                viewHolder.v1.setVisibility(View.VISIBLE);
            } else {
                viewHolder.tvOrderNo.setText(dataBeans.get(position).getRackNo());
                viewHolder.v1.setVisibility(View.GONE);
            }
            viewHolder.ll.setBackgroundColor(GetColorValue(dataBeans.get(position).getColorBg()));


            if (!TextUtils.isEmpty(dataBeans.get(position).getOrderNo())) {
                viewHolder.tvOrderNo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        orderDetialOn.Onclick(position);
                    }
                });
            }
        }
        return convertView;
    }

    public interface OrderDetialOn {
        void Onclick(int position);
    }

    //从字符串转颜色
    public int GetColorValue(String paramColor) {
        int PenColor = Color.WHITE;
        String StrPenColor = paramColor;
        if ((StrPenColor.indexOf("#") == 0) && (StrPenColor.length() == 7)) {
            int testColor = Integer.parseInt(StrPenColor.substring(1), 16);
            int red = (testColor & 0xff0000) >> 16;
            int green = (testColor & 0x00ff00) >> 8;
            int blue = (testColor & 0x0000ff);
            PenColor = Color.rgb(red, green, blue);
        } else {
            if (isNumer(StrPenColor)) {
                int testColor = Integer.parseInt(StrPenColor);
                int red = (testColor & 0xff0000) >> 16;
                int green = (testColor & 0x00ff00) >> 8;
                int blue = (testColor & 0x0000ff);
                PenColor = Color.rgb(red, green, blue);
            } else {
                if (StrPenColor.equals("Black") == true)
                    PenColor = Color.BLACK;
                if (StrPenColor.equals("Blue") == true)
                    PenColor = Color.BLUE;
                if (StrPenColor.equals("Yellow") == true)
                    PenColor = Color.YELLOW;
                if (StrPenColor.equals("Red") == true)
                    PenColor = Color.RED;
                if (StrPenColor.equals("Green") == true)
                    PenColor = Color.GREEN;
                if (StrPenColor.equals("Gray") == true)
                    PenColor = Color.LTGRAY;
                if (StrPenColor.equals("White") == true)
                    PenColor = Color.WHITE;
            }
        }
        return PenColor;
    }

    //判断是否为数字，不然不转
    public static boolean isNumer(String str) {
        try {
            new BigDecimal(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }



    class ViewHolder {
        @BindView(R.id.tv_placeNo)
        TextView tvPlaceNo;
        @BindView(R.id.v1)
        View v1;
        @BindView(R.id.tv_orderNo)
        TextView tvOrderNo;
        @BindView(R.id.ll)
        LinearLayout ll;
        @BindView(R.id.mylistitem)
        LinearLayout mylistitem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
